package vehicle

import (
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"server/db"
)

type VehicleDao struct {
	db *db.MongoMiddleware
}

// Constants
const (
	TBL_VEHICLE = "t_vehicle"
	// Max allowed data per query
	DATA_MAX_IN = 100
)

func NewVehicleDao() *VehicleDao {
	return &VehicleDao{
		db: db.Db,
	}
}

func (v *VehicleDao) removeCar(userId string, vTid int64) error {
	filter := bson.M{
		"user_id": userId,
		"vt_id":   vTid,
	}
	err := v.db.Delete(TBL_VEHICLE, filter)
	if err != nil {
		return fmt.Errorf("删除失败")
	}
	return nil
}

func (v *VehicleDao) getVehicleInfo(userId string, vTid int64) (Vehicle, error) {
	var result Vehicle
	filter := bson.M{
		"user_id": userId,
		"vt_id":   vTid,
	}
	err := v.db.FindOne(TBL_VEHICLE, filter, &result)
	if err != nil {
		return result, err
	}
	return result, nil
}
func (v *VehicleDao) getVehiclList(userId string) ([]Vehicle, error) {
	var result []Vehicle
	filter := bson.M{
		"user_id": userId,
	}
	err := v.db.Find(TBL_VEHICLE, filter, &result)
	if err != nil {
		return result, err
	}
	return result, nil
}
func (v *VehicleDao) insert(userId string, vehicle Vehicle) error {
	vehicle.UserId = userId
	return v.db.Insert(TBL_VEHICLE, vehicle)

}
func (v *VehicleDao) updateLevel(userId string, vTid, level int64) error {
	filter := bson.M{
		"user_id": userId,
		"vt_id":   vTid,
	}
	update := bson.M{
		"$set": bson.M{
			"level": level,
		},
	}
	err := v.db.Update(TBL_VEHICLE, filter, update)
	if err != nil {
		return err
	}

	return nil
}
func (v *VehicleDao) updateSkill(userId string, vTid int64, skillList []SkillInfo) error {
	filter := bson.M{
		"user_id": userId,
		"vt_id":   vTid,
	}
	update := bson.M{
		"$set": bson.M{
			"skill_list": skillList,
		},
	}
	err := v.db.Update(TBL_VEHICLE, filter, update)
	if err != nil {
		return err
	}

	return nil
}
